package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupMapper;
import com.itheima.dao.CheckItemMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exception.CheckGroupException;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @version V1.0
 * @author: ljx
 * @date: 2020/01/11 20:56
 * @description:
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public void add(CheckGroup checkGroup) {
        //在t_checkgroup表中添加基本信息,并回显添加完成后的主键
        checkGroupMapper.addCheckGroup(checkGroup);
        Integer groupId = checkGroup.getId();//获取回填的主键值
        List<Integer> checkitemIds = checkGroup.getCheckitemIds();//获取检查组中勾选的检查项的id
        setCheckGroupAndCheckItem(checkitemIds, groupId);//将检查组和检查项的对应关系添加到中间表

    }

    /**
     * 将检查组与检查项的对应关系添加到中间表
     *
     * @param checkitemIds 检查组中包含的检查项的id集合
     * @param groupId      检查组的id
     */
    private void setCheckGroupAndCheckItem(List<Integer> checkitemIds, Integer groupId) {
        //在t_checkgroup_checkitem表中添加检查组和检查项的对应关系
        if (checkitemIds.size() > 0) {
            ArrayList<Map> mapList = new ArrayList<>();
            for (Integer cid : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("groupId", groupId);
                map.put("itemId", cid);
                mapList.add(map);
            }
            checkGroupMapper.setCheckGroupIdAndCheckItemId(mapList);
        }
    }

    /**
     * 根据检查组id查询检查组的基本信息
     *
     * @param id 检查组id
     * @return result结果
     */
    @Override
    public Result findGroupMsg(Integer id) {
        //查询检查组基本信息
        CheckGroup checkGroup = checkGroupMapper.findBasicGroupMsg(id);
        //查询检查组中包含的所有检查项的id
        List<Integer> checkitemIds = checkGroupMapper.findGroupItem(id);
        //查询所有检查项信息并回显
        List<CheckItem> tableData = checkItemMapper.findAllCheckItem();
        checkGroup.setCheckitemIds(checkitemIds);
        checkGroup.setCheckItems(tableData);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkGroup);

    }

    /**
     * 分页查询检查组的基本信息
     *
     * @param pageBean 封装了分页查询所需参数的对象
     * @return 分页查询结果
     */
    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        Page page = PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        List<CheckGroup> groupList = checkGroupMapper.findPage(pageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(), groupList);
        pageResult.setResult(new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS));
        return pageResult;
    }

    @Override
    public Result edit(CheckGroup checkGroup) {
        //更新基本信息
        checkGroupMapper.update(checkGroup);
        //删除中间表原有关系
        checkGroupMapper.deleteGroupAndItemRel(checkGroup.getId());
        //在中间表中添加新的关系
        setCheckGroupAndCheckItem(checkGroup.getCheckitemIds(), checkGroup.getId());

        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    @Override
    public Result delete(Integer id) {
        Integer count = checkGroupMapper.findCountRelWithMeal(id);
        if(count>0){
            throw  new CheckGroupException("该检查组被检查套餐引用，不能删除，请先删除套餐后再删除该检查组！");
        }else{
            //删除检查项与检查组的对应关系
            checkGroupMapper.deleteGroupAndItemRel(id);
            //删除检查组的基本信息
            checkGroupMapper.deleteGroupBasicMsg(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }

    }

    @Override
    public Result findAll() {
        List<CheckGroup> groupList = checkGroupMapper.findAll();
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,groupList);
    }
}
